<?php

namespace app\admin\controller\microend;

use app\common\controller\Backend;
use think\Db;

/**
 * 派克认证管理
 *
 * @icon fa fa-circle-o
 */
class UserReal extends Backend
{
    
    /**
     * Real模型对象
     * @var \app\common\model\user\Real
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\user\Real;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $join[] = ["sd_user_attribute e","e.uid = l.uid","left"];

            $join[] = ["sd_user r","r.uid = l.uid","left"];

            $field = "l.*,real_id as id";//user_real 字段

            $field .= ",r.username,r.tao_bao_no,r.mobile,r.balance,r.role";//user 字段


            $field .= ",e.task_day_count,e.month_task_count,e.task_limit_cash,e.single_status,e.cash_status,e.remarks";//user_attribute 字段



            $total = $this->model
                ->alias("l")
                ->join($join)
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->alias("l")
                ->join($join)
                //->with('userAttribute,user')
                ->field($field)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 认证身份证
     *
     * type:2
     * 通过
     *
     * type：3
     * 驳回
     *
     * type 4
     * 禁用
     *
     */
    public function real($ids=NULL){
        $row = $this->model->with('userAttribute,user,authReal')->field('*,uid as id')->find(['real_id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {

            $params = $this->request->post();

            if ($params)
            {
               // Db::startTrans();
                try {

                    $data = [];
                    if($params["type"]==2){
                        $data = ["real_status"=>"2"];
                    }elseif($params["type"]==3){
                        $data = ["real_status"=>"3"];

                    }elseif($params["type"]==4){
                        $data = ["real_status"=>"4"];
                    }
                    $op =  $this->model->add_or_up($data,['real_id' => $params["real_id"]]);//驳回

                    if($params["type"]==2){
                        $uid = $params["uid"];
                        $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                        $msg = [
                            "condition"=>2,
                            "touser"=>$openid
                        ];
                        $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                        $MiniMsg->sendMsg();//发送短信模板身份证审核成功
                    }elseif($params["type"]==3){
                        $uid = $params["uid"];
                        $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                        $msg = [
                            "condition"=>1,
                            "touser"=>$openid
                        ];
                        $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                        $MiniMsg->sendMsg();//发送短信模板身份证审核驳回
                    }
                    if($params["type"]==4){
                        Db::name("user")->where(['uid' => $params["uid"]])->update(["status"=>0]);
                    }
                    if($op) {
                        renderJson("成功", 1);
                    }else{
                        renderJson("失败", 1);
                    }
                }catch (\Exception $e) {
                    renderJson("失败", 0);
                }
            }
            renderJson("失败",0);
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();

    }
    

}
